package com.uday.myworld;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;

import android.app.Activity;
import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class found extends ListActivity 
{
	 ArrayList<String> full_lv_arr=new ArrayList<String>();
	public void onCreate(Bundle savedInstanceState) {
        
		
		Context context = getApplicationContext();
		super.onCreate(savedInstanceState);
        setContentView(R.layout.found);
        
        
        String location= getIntent().getExtras().getString("location").toString();        
        TextView tv = (TextView) findViewById(R.id.flocation);        
        tv.setText(location);
        
        
        ArrayAdapter<String> adapter;
        ArrayList<String> lv_arr=new ArrayList<String>();
       
       // ListView lv1=(ListView)findViewById(R.id.list);
    
     //lv1.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1 , lv_arr));
    
        
        
        
        
        
              
              
        ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
        nameValuePairs.add(new BasicNameValuePair("location", location));    
                
       // Toast.makeText(context,"before calling", Toast.LENGTH_LONG).show();
        try {
      	  HttpClient httpclient = new DefaultHttpClient();
         HttpPost httppost = new HttpPost("http://10.10.10.121:8084/Myworld/view.jsp");
      	//HttpPost httppost = new HttpPost(R.string.url+"view.jsp");
          httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
          HttpResponse response = httpclient.execute(httppost);
        // Toast.makeText(context,"after calling", Toast.LENGTH_LONG).show();
          HttpEntity entity = response.getEntity();                
          InputStream is = entity.getContent();
          BufferedReader in = new BufferedReader(new InputStreamReader(is));
      	String inputLine,json="";
      	while ((inputLine = in.readLine()) != null) {
      		json=json+inputLine;
      	}		
      	in.close();
        
          JSONArray json1=new JSONArray(json);
  		//String retrived="";  		
  		for(int i=0; i<json1.length();i++)
  		{
  			
  			full_lv_arr.add(json1.get(i).toString());
  			if(i%3==0)
  			{
  				lv_arr.add(json1.get(i).toString());
  			}
  			//=(String) json1.get(i);
  			//Toast.makeText(context,retrived, Toast.LENGTH_LONG).show();
  		}
  		
  		adapter= new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1 , lv_arr);
  		setListAdapter(adapter);
  		//Toast.makeText(context,"added", Toast.LENGTH_LONG).show();
        }
        catch(Exception e)
        {
        	Log.d("Error parsing data ",e.toString());
        }
       
	}
	
	protected void onListItemClick(ListView l, View v, int position, long id) {
		String item = (String) getListAdapter().getItem(position); 		  
		//Toast.makeText(getApplicationContext(),item + " selected"+position, Toast.LENGTH_SHORT).show();
		
		Intent i = new Intent(found.this,view.class);
		i.putExtra("location",item); 	
		i.putExtra("position", position);
		i.putExtra("Flist",full_lv_arr );
		startActivity(i);
		
		
		
	}
	  @Override
		public boolean onCreateOptionsMenu(Menu menu) {
			MenuInflater inflater = getMenuInflater();
			inflater.inflate(R.layout.menu, menu);
			return true;
		}

		@Override
		public boolean onOptionsItemSelected(MenuItem item) {
			switch (item.getItemId()) {
			case R.id.exit:
				//finish();
				finishActivity(1);
			
				return true;
			default:
				return super.onOptionsItemSelected(item);
			}
		}
	

}
